package com.k.kpp.web.easyexcel2.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import com.k.kpp.web.easyexcel2.annotation.ExcelSelectedResolve;
import org.apache.poi.ss.usermodel.DataValidation;
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.DataValidationHelper;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddressList;

import java.util.Map;

/**
 * handler
 *
 * @author ljw
 */
public class ExcelDropDownHandler implements SheetWriteHandler {

    /**
     * 下拉框值
     */
    private Map<Integer, String[]> dropDownMap;
    private Map<Integer, ExcelSelectedResolve> dropDownMapResolve;


    /**
     * 开始行
     */
    private Integer firstRow = 1;

    /**
     * 结束行
     */
    private Integer lastRow = 65534;

//	public ExcelDropDownHandler(Map<Integer, String[]> dropDownMap) {
//		this.dropDownMap = dropDownMap;
//	}

    public ExcelDropDownHandler(Map<Integer, ExcelSelectedResolve> dropDownMapResolve) {
        this.dropDownMapResolve = dropDownMapResolve;
    }

    public ExcelDropDownHandler(Map<Integer, String[]> dropDownMap, Integer firstRow, Integer lastRow) {
        this.dropDownMap = dropDownMap;
        this.firstRow = firstRow;
        this.lastRow = lastRow;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        DataValidationHelper helper = sheet.getDataValidationHelper();
//		resolveDropDownMap(sheet, helper);
        resolveDropDownMapResolve(sheet, helper);
    }

    private void resolveSheetDropdown(String[] dropdownData, Sheet sheet, DataValidationHelper helper, int firstRow, int lastRow, int firstCol, int lastCol) {
        CellRangeAddressList cellRangeAddressList = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
        DataValidationConstraint constraint = helper.createExplicitListConstraint(dropdownData);
        DataValidation dataValidation = helper.createValidation(constraint, cellRangeAddressList);
        // 限制输入校验
        dataValidation.setErrorStyle(0);
        dataValidation.setShowErrorBox(true);
        dataValidation.setSuppressDropDownArrow(true);
        dataValidation.createErrorBox("Tip:", "Dropdown content required");
        sheet.addValidationData(dataValidation);
    }

    private void resolveDropDownMapResolve(Sheet sheet, DataValidationHelper helper) {
        dropDownMapResolve.forEach((celIndex, dropResolve) -> resolveSheetDropdown(dropResolve.getSource(), sheet, helper, dropResolve.getFirstRow(), dropResolve.getLastRow(), celIndex, celIndex));
    }

    private void resolveDropDownMap(Sheet sheet, DataValidationHelper helper) {
        dropDownMap.forEach((celIndex, values) -> resolveSheetDropdown(values, sheet, helper, firstRow, lastRow, celIndex, celIndex));
    }
}